package com.dmooo.yxmmerchant.view.model;

import android.content.Context;

import com.common.net.entity.SubjectPost;
import com.common.net.listener.HttpOnNextListener;
import com.common.net.mywidget.LoadingDialog;
import com.common.net.subscribers.ProgressSubscriber;
import com.common.util.LogUtil;
import com.dmooo.yxmmerchant.common.Config;
import com.dmooo.yxmmerchant.http.HttpManager;
import com.dmooo.yxmmerchant.view.contract.RegisterContract;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import cz.msebera.android.httpclient.Header;

public class RegisterModel implements RegisterContract.RegisterMdl {
    private Context context;
    private LoadingDialog loadingDialog;
    public RegisterModel(Context context){
        this.context=context;
        loadingDialog=new LoadingDialog(context);
    }
    @Override
    public void getPhoneCode(String phone, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("phone",phone);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"获取验证码"),map);
        HttpManager.getInstance().sendUserRegister(post);
    }

    @Override
    public void doRegister(String phone, String code, String pwd, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("phone",phone);
        map.put("code",code);
        map.put("pwd",pwd);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"注册中"),map);
        HttpManager.getInstance().userRegiter(post);
    }
    /**
     * phone	char(11)
     * merchant_type	char(1)
     * merchant_type_id	int(10)
     * merchant_type_id2	int(10)
     * merchant_name	varchar(20)
     * merchant_avatar	file
     * service_tel	varchar(30)
     * province	varchar(30)
     * city	varchar(30)
     * county	varchar(30)
     * detail_address	varchar(100)
     * lng_lat	varchar(100)
     * business_day_begin	char(1)
     * business_day_end	char(1)
     * business_hours_begin	varchar(20)
     * business_hours_end	varchar(20)
     */
    @Override
    public void submitMerchantProfile(String phone, String merchant_type, String merchant_name, File avatar, String service_tel, String business_day_begin, String business_day_end, String province, String city, String county, final HttpOnNextListener listener) {
        try {
            RequestParams requestParams = new RequestParams();
            AsyncHttpClient client1 = new AsyncHttpClient();
            requestParams.put("phone", phone);
            requestParams.put("merchant_type", merchant_type);
            requestParams.put("merchant_name", merchant_name);
            requestParams.put("avatar", avatar);
            requestParams.put("detail_address",Config.DETAIL_ADDRESS);
            requestParams.put("service_tel", service_tel);
            requestParams.put("business_day_begin", business_day_begin);
            requestParams.put("business_day_end", business_day_end);
            requestParams.put("province", province);
            requestParams.put("city", city);
            requestParams.put("county", county);
            client1.post(Config.APP_INTERFACE_BASE_URL + "/merchant.php?c=MerchantAccount&a=register_detail", requestParams, new TextHttpResponseHandler() {

                @Override
                public void onStart() {
                    super.onStart();
                    loadingDialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    loadingDialog.dismiss();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    listener.onFail(responseString);
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    LogUtil.e("backdata",responseString);
                    try {
                        JSONObject object1=new JSONObject(responseString);
                        if(object1.getInt("code")==0){
                            listener.onNext("");
                        }else{
                            listener.onFail(object1.getString("msg"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        }catch (FileNotFoundException e){
            LogUtil.e("backdata",e.toString());
        }
    }

    @Override
    public void submitId(String phone, File business_license, String artificial_person, String artificial_person_id_num, File artificial_person_id_img1, File artificial_person_id_img2, final HttpOnNextListener listener) {
        try {
            RequestParams requestParams = new RequestParams();
            AsyncHttpClient client1 = new AsyncHttpClient();
            requestParams.put("phone", phone);
            requestParams.put("business_license", business_license);
            requestParams.put("artificial_person", artificial_person);
            requestParams.put("artificial_person_id_num", artificial_person_id_num);
            requestParams.put("artificial_person_id_img1", artificial_person_id_img1);
            requestParams.put("artificial_person_id_img2", artificial_person_id_img2);
            client1.post(Config.APP_INTERFACE_BASE_URL + "/merchant.php?c=MerchantAccount&a=register_detail2", requestParams, new TextHttpResponseHandler() {
                @Override
                public void onStart() {
                    super.onStart();
                    loadingDialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    loadingDialog.dismiss();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    listener.onFail(responseString);
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    LogUtil.e("backdata",responseString);
                    try {
                        JSONObject object1=new JSONObject(responseString);
                        if(object1.getInt("code")==0){
                            listener.onNext("");
                        }else{
                            listener.onFail(object1.getString("msg"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        }catch (FileNotFoundException e){
            LogUtil.e("backdata",e.toString());
        }
    }
}
